home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Network Support Library
/
RoseWare - Network Support Library.iso
/
apidev
/
locks.arc
/
DOS_LOCK.C
next >
Wrap
Text File
|
1989-04-19
|
3KB
|
157 lines
/* Functions for physical record locking/unlocking for NETWORK files */
#include <dos.h>
#include <fcntl.h>
#include <stdio.h>
struct temp
{
int handle;
long start;
long blk_size;
} lock_log[256];
int locks_on=0;
/* Return lo word of a long
*/
unsigned lo(l)
long l;
{
return(l&0xFFFF);
};
/* Return hi word of a long
*/
unsigned hi(l)
long l;
{
return(l>>16);
};
/* Set locking mode
*/
set_lock_mode()
{
struct REGPACK regs;
regs.r_ax = 0xC601;
intr(0x21,®s);
};
/* Log and lock a record
*/
int dos_lock(handle,start,blk_size)
int handle;
long start;
long blk_size;
{
if(lock(handle,start,blk_size))
return(0);
else
{
lock_log[locks_on].handle = handle;
lock_log[locks_on].start = start;
lock_log[locks_on].blk_size = blk_size;
locks_on++;
}
return(1);
};
/* Clear the records that have been logged or locked
*/
int clear_recs()
{
int i;
for (i=0;i!=locks_on;i++)
unlock(lock_log[i].handle,lock_log[i].start,lock_log[i].blk_size);
locks_on = 0;
return(1);
};
main()
{
char c;
int start;
int blk_size;
int timeout;
int handle;
char buff[256];
int i;
int flgs;
handle = open("test.dat",O_RDWR|O_DENYNONE|O_BINARY);
ioctl(handle,11,1,1);
while (!0)
{
clrscr();
printf("Enter (L)ock,(C)lear locks,(R)ead,(W)rite,(S)how,(Q)uit :");
c=getch();
printf("\n");
if ((c==27)||(c=='q')||(c=='Q'))
exit(0);
if ((c=='l')||(c=='L'))
{
printf("Enter start : ");
scanf("%d",&start);
printf("Enter blk_size : ");
scanf("%d",&blk_size);
if (!dos_lock(handle,(long)start,(long)blk_size))
{
printf("LOGGING LOCKING ERROR");
getch();
}
}
if ((c=='c')||(c=='C'))
{
if(!clear_recs())
{
printf("CLEARING ERROR");
getch();
}
}
if ((c=='r')||(c=='R'))
{
printf("Enter start : ");
scanf("%d",&start);
printf("Enter blk_size : ");
scanf("%d",&blk_size);
lseek(handle,(long)start,SEEK_SET);
i = read(handle,&buff,blk_size);
if (i!=blk_size)
{
printf("READ ERROR\n");
getch();
}
for (i=0;i!=blk_size;i++)
putch(buff[i]);
printf("\n");
getch();
}
if ((c=='w')||(c=='W'))
{
printf("Enter start : ");
scanf("%d",&start);
printf("Enter blk_size : ");
scanf("%d",&blk_size);
lseek(handle,(long)start,SEEK_SET);
printf("Enter in the %d characters\n",blk_size);
for (i=0;i!=blk_size;i++)
{
buff[i]=getch();
putch(buff[i]);
}
printf("\n");
write(handle,&buff,blk_size);
}
if ((c=='s')||(c=='S'))
{
for (i=0;i!=locks_on;i++)
printf("%d %d %d\n",lock_log[i].handle,(int)lock_log[i].start,(int)lock_log[i].blk_size);
getch();
}
}
};